package org.douxiaoban.client.adapter;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import org.douxiaoban.client.R;
import org.douxiaoban.client.activity.CollectionBookListActivity;
import org.douxiaoban.client.api.CollectionRecord;
import org.douxiaoban.client.util.ThreadTool;

import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: huangzhongkun
 * Date: 11-5-23
 * Time: 下午10:24
 */
public class CollectionListAdapter extends BaseAdapter{
    public ArrayList<CollectionRecord> _crs = new ArrayList<CollectionRecord>();
    private Activity _context;
    private String collectionType;
    private ThreadTool _thread;
    private Handler _handler;
    private ListView listView;
    private AsyncImageLoader asyncImageLoader;

    private class ViewHolder{
        TextView titleView;
        TextView tagsView;
        TextView attributeView;
        TextView updateDateView;
        TextView summaryView;
        RatingBar ratingBar;
        ImageView iconImage;

    }

    public CollectionListAdapter(Activity context,Handler handler,String collectionType
                                  ,ListView listView){
        this._context = context;
        this.collectionType = collectionType;
        _thread = ThreadTool.instace(context);
        _handler = handler;

        this.listView = listView;
        asyncImageLoader = new AsyncImageLoader();
    }

    @Override
    public int getCount() {
        return _crs.size();
    }

    @Override
    public Object getItem(int index) {
             return _crs.get(index);
     }

    @Override
    public long getItemId(int index) {
        return index;
    }


    @Override
    public View getView(int index, View converView, ViewGroup viewGroup) {

        if(_crs.get(index) == null)return null ;

        ViewHolder viewHolder;
        final CollectionRecord record = _crs.get(index);

         if(converView == null){
            LayoutInflater layoutInflater = LayoutInflater.from(_context);
            converView = layoutInflater.inflate(R.layout.collections_item,null);

            viewHolder = new ViewHolder();


           viewHolder.titleView =
                   (TextView)converView.findViewById(R.id.wish_read_book_title);
           viewHolder.tagsView =
                   (TextView)converView.findViewById(R.id.wish_read_book_tags);
           viewHolder.ratingBar =
                   (RatingBar)converView.findViewById(R.id.wish_read_book_rating);
           viewHolder.iconImage =
                   (ImageView)converView.findViewById(R.id.wish_read_book_icon);
           viewHolder.attributeView =
                   (TextView)converView.findViewById(R.id.wish_read_book_attribute);
           viewHolder.summaryView =
                   (TextView)converView.findViewById(R.id.wish_read_book_summary);
           viewHolder.updateDateView =
                   (TextView)converView.findViewById(R.id.wish_read_book_updatedate);

            converView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder)converView.getTag();
        }

         viewHolder.titleView.setText(record.title);

           if(record.rating != 0)
                viewHolder.ratingBar.setRating(record.rating);
            else
               viewHolder.ratingBar.setVisibility(0);

           if(record.tags.size() != 0){
               String tag = "";
               for(String item:record.tags){
                   tag += item+" ";
               }
               viewHolder.tagsView.setText("标签: "+tag);
           }else {
               viewHolder.tagsView.setVisibility(View.GONE);
           }
              //  viewHolder.tagsView.setVisibility(View.INVISIBLE);

        //TODO:是否有条目的attribute 不为0 也不为 6
        if(record.attribute_content!=null&& record.attribute_content.size() >0){
            String attribute="";
            int i = 0;
            for(String item:record.attribute_content){
                int t = item.indexOf("http");
                if(item.indexOf("http")>=0)
                    continue;

                if(i>1)
                  attribute += "/"+item;
                  i++;
            }
            if(attribute.length()>100)
                attribute = attribute.substring(0,100)+"...";
            viewHolder.attributeView.setText(attribute);
        }else
            viewHolder.attributeView.setVisibility(View.GONE);

        viewHolder.updateDateView.setText(record.update_date);

        if(record.summary !=null)
          viewHolder.summaryView.setText("    \"" + record.summary + "\"");
        else
          viewHolder.summaryView.setVisibility(View.GONE);

        final ImageView imageView = viewHolder.iconImage;
         imageView.setTag(record.icon_url);

//        if(record.icon_bitmap != null){
//            viewHolder.iconImage.setImageBitmap(record.icon_bitmap);
//            record.icon_download_status = true;
//        }

//        if(!record.icon_downloading && record.icon_bitmap == null){
//            record.icon_downloading = true;
//            _thread.startThreadImageDownload(collectionType,record.icon_url,_handler,index);
//        }

        Drawable cacheImage = asyncImageLoader.loadDrawable(record.icon_url,new AsyncImageLoader.ImageCallback(){
            @Override
            public void imageLoaded(Drawable imageDrawable, String imageUrl) {
                ImageView imageViewByTag = (ImageView)listView.findViewWithTag(record.icon_url);
                LinearLayout.LayoutParams params =
                        new LinearLayout.LayoutParams(80,LinearLayout.LayoutParams.WRAP_CONTENT);
                if(imageViewByTag !=null){
                    imageViewByTag.setLayoutParams(params);
                    imageViewByTag.setImageDrawable(imageDrawable);
                }
            }
        });


        if(cacheImage ==null){

        LinearLayout.LayoutParams params =
                        new LinearLayout.LayoutParams(80,LinearLayout.LayoutParams.WRAP_CONTENT);
            imageView.setLayoutParams(params);
            imageView.setImageResource(R.drawable.default_bg_image_73);
        }else{
            imageView.setImageDrawable(cacheImage);
        }



        return converView;
    }

    public void changeData(){
        Log.v("collectionAdatpter","changData:"+_crs.toString());
        this.notifyDataSetChanged();
    }
}
